<template>
  <el-dialog
    :title="{ add: '新增用户', edit: '编辑用户' }[mode]"
    width="600px"
    :visible.sync="show"
    @close="closeEvent"
  >
    <div style="width: 500px">
      <el-form ref="form" label-width="80px" :model="form" :rules="rules">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>

        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email"></el-input>
        </el-form-item>

        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone"></el-input>
        </el-form-item>

        <el-form-item label="角色" prop="role_id">
          <!-- <el-input v-model="" style="width: 200px"></el-input> -->
          <el-select placeholder="请选择" v-model="form.role_id">
            <el-option
              v-for="item in downList"
              :key="item.role_id"
              :label="item.name"
              :value="item.role_id"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="状态" prop="status">
          <!-- <el-input v-model="form.status"></el-input> -->
          <el-select placeholder="请选择" v-model="form.status">
            <el-option
              v-for="item in on_off"
              :key="item.status"
              :label="item.name"
              :value="item.status"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="用户备注" prop="remark">
          <el-input v-model="form.remark"></el-input>
        </el-form-item>
      </el-form>
    </div>

    <div style="text-align: center">
      <el-button type="primary" @click="submit">确定</el-button>
      <el-button @click="show = false">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { userListAddAPI, userListEditAPI } from "@/api/user";
export default {
  data() {
    return {
      show: false,
      roleValue: "",
      statusValue: "",
      mode: "",
      form: {
        username: "", //	是	string	用户名
        email: "", //	是	string	邮箱
        phone: "", //	是	string	手机号
        role_id: "", //	是	string	角色 、2 管理员、3 老师、4 学生
        status: "", //	否	string	1(启用)0(禁用)
        remark: "", //	否	string	备注
      },
      rules: {
        username: [{ required: true, message: "必填", trigger: "blur" }],
        email: [{ required: true, message: "必填", trigger: "blur" }],
        phone: [{ required: true, message: "必填", trigger: "blur" }],
        role_id: [{ required: true, message: "必填", trigger: "blur" }],
        status: [{ required: true, message: "必填", trigger: "blur" }],
      },
      downList: [
        { name: "管理员", role_id: "1" },
        { name: "老师", role_id: "2" },
        { name: "学生", role_id: "3" },
      ],
      on_off: [
        { name: "启用", status: "1" },
        { name: "禁用", status: "0" },
      ],
    };
  },
  methods: {
    submit() {
      this.$refs.form.validate(async (result) => {
        if (result) {
          if (this.mode === "add") {
            await userListAddAPI(this.form);
            this.$message.success("新增用户成功");
          } else if (this.mode === "edit") {
            await userListEditAPI(this.form);
            this.$message.success("修改用户成功");
          }
          this.show = false;
          this.$emit("getData");
        }
      });
    },
    closeEvent() {
      this.$refs.form.resetFields();
      this.form = {
        username: "", //	是	string	用户名
        email: "", //	是	string	邮箱
        phone: "", //	是	string	手机号
        role_id: "", //	是	string	角色 、2 管理员、3 老师、4 学生
        status: "", //	否	string	1(启用)0(禁用)
        remark: "", //	否	string	备注
      };
    },
  },
};
</script>

<style></style>
